﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Wyn.PMS.Entity.Entities;
using Wyn.PMS.Entity.IDataAccess;

namespace Wyn.PMS.Test.Cache
{
    public class SearchInvoice
    {
        private IUnityContainer container = UnityContainerService.GetContainer();

        private string _hotelId;

        public SearchInvoice(string hotelId)
        {
            _hotelId = hotelId;
        }

        public void Search()
        {
            bool isContinue = true;

            while (isContinue)
            {
                Console.Write("\r\n 请输入要查询的员工ID或姓名：");

                string employee = Console.ReadLine();

                Console.Write("\r\n 请输入要查询的日期,如2012-10-4：");

                string dateStr = Console.ReadLine();

                DateTime date;

                if (!DateTime.TryParse(dateStr, out date))
                {
                    continue;
                }

                DateTime beginDate=date.Date;

                DateTime endDate=date.Date.AddHours(23);

                var invoiceRepository = container.Resolve<IInvoiceRepository>();

                IList<Invoice> invoices = invoiceRepository
                    .BatchFind(_hotelId, o => o.HotelID == _hotelId
                                              && o.CreateTime > beginDate
                                              && o.CreateTime <= endDate
                                              && o.EmployeeId.Contains(employee));

                DisplayInvoice(invoices,date);

                Console.Write("请问是否查询其他的发票(Y/N)？");
                    
                string yesOrNo = Console.ReadKey().KeyChar.ToString().ToUpper();

                if (yesOrNo.ToUpper() != "Y") isContinue = false;
            }
        }

        private void DisplayInvoice(IList<Invoice> invoices, DateTime date)
        {
            if (invoices == null || invoices.Count <= 0)
            {
                Console.WriteLine("没有查询到{0}发票啊，请继续努力。",date);
                return;
            }

            Console.WriteLine("★ 房号 | 姓名 | 发票 | 消费 | 操作人| 班次 | 协议 | 租类 | 入住期间");

            foreach (var item in invoices)
            {
                Console.WriteLine(" {0} | {1} | {2} | {3} | {4} | {5} | {6} \r\n          {7} | {8} - {9} ",
                                  item.RoomNo, item.GuestName, 
                                  item.VoiceSum, item.DebitSum,
                                  item.EmployeeId, item.WorkShift,
                                  item.AgrNo, item.CheckInType,
                                  item.CheckInDate.ToString(),
                                  item.CheckOutDate.ToString());
            }

            Console.WriteLine("当前已开发票数:{0}",invoices.Sum(o=>o.VoiceSum));
            
        }
    }
}
